#!/bin/bash -e

# Copyright 2021 Flant JSC
# Licensed under the Deckhouse Platform Enterprise Edition (EE) license. See https://github.com/deckhouse/deckhouse/blob/main/ee/LICENSE


for f in $(find /frameworks/shell/ -type f -iname "*.sh"); do
  source $f
done

function __config__() {
  cat << EOF
    configVersion: v1
    kubernetes:
    - name: public_services
      apiVersion: v1
      kind: Service
      group: main
      keepFullObjectsInMemory: false
      labelSelector:
        matchLabels:
          federation.istio.deckhouse.io/public-service: ""
      jqFilter: |
        {
          "hostname": (.metadata.name + "." + .metadata.namespace + ".svc.$CLUSTER_DOMAIN"),
          "ports": [.spec.ports[] | {"name": .name, "port": .port}]
        }
EOF
}

function __main__() {
  context::jq '
    [
      .snapshots.public_services[].filterResult
    ]' > /metadata/services-array.json
}

hook::run "$@"
